package encryption;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Encryption {

	private static MessageDigest md5;
	private static boolean init = false;

	private static void initialize() {
		if (!init) {
			init = true;
			try {
				md5 = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {

			}
		}
	}

	public static String encrypt(String value) {
		initialize();
		if (md5 == null)
			return value;
		
		md5.update(value.getBytes());
		return convertByteToString(md5.digest());
	}
	
	private static String convertByteToString(byte[] bytes){
		String value = "";
		
		for (byte b : bytes)
			value += Integer.toHexString(0xFF & b);
		
		return value;
	}
}
